package com.dfrz.dao;


import com.dfrz.entry.VipUser;
import com.dfrz.util.JdbcUtils;
import com.mysql.cj.util.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class VipUserDao {
    /**/

    /*添加用户*/
    public int insertUser(VipUser vipuser){
        int count = 0;
        String sqlStr="insert into vipinfo(userid,vipdiscountPower,level)" +
                "values(?,?,?)";
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        try {
           queryRunner.update(sqlStr,vipuser.getId(),vipuser.getDiscount(),vipuser.getLevel());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return count;
    }

    /*逻辑删除用户*/
    public int deleteUserById(Integer userid){
        int count = 0;
        String sqlStr="update vipinfo  set status=1 where userid=?";
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,userid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
    public List<VipUser> queryUserByPage(Integer userid, int limit, int startNum) {
        List<VipUser> listUser = null;
        String str = "";
        if (userid!=null) {
            str = "select userinfo.*,vipinfo.vipid,vipinfo.vipdiscountPower,vipinfo.level from userinfo, vipinfo where userid=? and status=1 limit ?,?";

            QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
            try {
                listUser = queryRunner.query(str, new BeanListHandler<VipUser>(VipUser.class),userid, startNum, limit);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } else {
            str = "select userinfo.*,vipinfo.vipid,vipinfo.vipdiscountPower,vipinfo.level from userinfo,vipinfo where status=1 limit ?,?";
            QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
            try {
                listUser = queryRunner.query(str, new BeanListHandler<VipUser>(VipUser.class), startNum, limit);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return listUser;
    }
}
